/**
 * ****************************************************************************
 * Project: RoboBridge
 * ----------------------------------------------------------------------------
 * -------------------------- [c]2012 - KrawallMann ---------------------------
 * ---------------------- http://www.krawallmann.de.tc/ -----------------------
 * ****************************************************************************
 * File: DEVICE_ID_LCD03.java Package: tc.de.krawallmann.robobridge.devices.implementation
 * Version: 1.0 Author(s): KrawallMann
 * ****************************************************************************
 * Description:
 * ****************************************************************************
 */
package tc.de.krawallmann.robobridge.devices.implementation;

import java.util.Arrays;

import tc.de.krawallmann.robobridge.communication.Command;
import tc.de.krawallmann.robobridge.communication.CommandType;
import tc.de.krawallmann.robobridge.devices.AbstractDevice;
import tc.de.krawallmann.robobridge.devices.Robot;

/**
 * The Class DEVICE_ID_LCD03 represents a 4x20 display made by Devantech.
 * 
 * @author KrawallMann
 * @version 1.0
 */
public class LCD03 extends AbstractDevice {

	/** The backlight on. */
	private boolean backlightOn;

	/** The texts. */
	private transient final String[] texts = {
			"", "", "", ""
	};

	/** The updated. */
	private transient final boolean[] updated = {
			false, false, false, false
	};

	/**
	 * Instantiates a new DEVICE_ID_LCD03.
	 * 
	 * @param robot
	 *            the robot
	 * @param deviceId
	 *            the device id
	 */
	public LCD03(final Robot robot, final int deviceId) {
		super(robot, AbstractDevice.DEVICE_ID_LCD03,deviceId, "Devantech", "DEVICE_ID_LCD03");
	}

	/**
	 * Clear.
	 */
	public void clear() {
		for (int i = 0; i < 4; ++i) {
			texts[i] = "                    ";
			updated[i] = true;
		}
	}

	/**
	 * Clear line.
	 * 
	 * @param line
	 *            the line
	 */
	public void clearLine(final int line) {
		if (line < 1 || line > 4) {
			throw new IllegalArgumentException("Line must be within [1,4]");
		}
		texts[line - 1] = "";
		updated[line - 1] = true;
	}

	/**
	 * Gets the text.
	 * 
	 * @param line
	 *            the line
	 * @return the text
	 */
	public String getText(final int line) {
		if (line < 1 || line > 4) {
			throw new IllegalArgumentException("Line must be within [1,4]");
		}
		return texts[line - 1];
	}

	/**
	 * Checks if is backlight on.
	 * 
	 * @return true, if is backlight on
	 */
	public boolean isBacklightOn() {
		return backlightOn;
	}

	/*
	 * (non-Javadoc)
	 * @see
	 * basepackage.IncomingCommandListener#onCommandReceived(basepackage.Command
	 * )
	 */
	@Override
	public boolean onCommandReceived(final Command command) {
		return false;
	}

	/**
	 * Sets the backlight on.
	 * 
	 * @param backlightOn
	 *            the new backlight on
	 */
	public void setBacklightOn(final boolean backlightOn) {
		this.backlightOn = backlightOn;
		getRobot().queueOutgoingCommand(
				new Command(CommandType.UPDATE_ROBOT_DEVICE, false, getDeviceId(), 2,
						backlightOn ? 1 : 0));
	}

	/**
	 * Sets the text.
	 * 
	 * @param line
	 *            the line
	 * @param text
	 *            the text
	 */
	public void setText(final int line, final String text) {
		if (line < 1 || line > 4) {
			throw new IllegalArgumentException("Line must be within [1,4]");
		}
		if (text.length() > 20) {
			throw new IllegalArgumentException("Text is longer than 20 characters!");
		}
		texts[line - 1] = text;
		updated[line - 1] = true;
	}

	/*
	 * (non-Javadoc)
	 * @see tc.de.krawallmann.robobridge.devices.AbstractDevice#toString()
	 */
	@Override
	public String toString() {
		return "DEVICE_ID_LCD03 [deviceId=" + deviceId + ", manufacturer=" + manufacturer + ", model="
				+ model + ", backlightOn=" + backlightOn + ", texts=" + Arrays.toString(texts)
				+ "]";
	}

	/*
	 * (non-Javadoc)
	 * @see tc.de.krawallmann.robobridge.devices.AbstractDevice#update()
	 */
	@Override
	public void update() {
		super.update();
		for (int i = 0; i < 4; ++i) {
			if (updated[i]) {
				robot.queueOutgoingCommand(new Command(CommandType.UPDATE_ROBOT_DEVICE, false,
						getDeviceId(), 1, i + 1, texts[i]));
				updated[i] = false;
			}
		}

	}
}
